"""
 -*- coding: utf-8-*-
 @File: mgt_data_save.py
 @Author: chen
 @time: 2023/10/04/ 17:29
"""
import datetime
import os.path
import allure

from stage_L1.lim_project.page_po.utils.mgt_logger import logger

# timestamp = int(time.time())
now = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
cur_path = os.path.dirname(os.path.abspath(__file__))


def save_screenshot(dr):
    """图片文件=文件目录+文件名"""
    picture_dir = os.path.join(cur_path, "../exceptions/screen_shots/")
    if not os.path.exists(picture_dir):
        os.makedirs(picture_dir)
    picture_name = f"screen_shot_{now}.PNG"
    picture_file = os.path.join(picture_dir, picture_name)

    dr.save_screenshot(picture_file)
    logger.info("保存截图--->成功")
    allure.attach.file(picture_file, name="pictures", attachment_type=allure.attachment_type.PNG)
    logger.info("上传截图--->成功")


def save_page_source(dr):
    """源码文件=文件目录+文件名"""
    page_source_dir = os.path.join(cur_path, "../exceptions/page_sources/")
    if not os.path.exists(page_source_dir):
        os.makedirs(page_source_dir)
    page_source_name = f"page_source_{now}.HTML"
    page_source_file = os.path.join(page_source_dir, page_source_name)
    # print(page_source)
    with open(page_source_file, 'w', encoding="utf-8") as f:
        f.write(dr.page_source)
        logger.info("保存网页源码--->成功")
    allure.attach.file(page_source_file, name="page_source", attachment_type=allure.attachment_type.HTML)
    logger.info("上传网页源码--->成功")
